package com.seal.commons.quartz.autoConfiguration;

import com.seal.commons.quartz.properties.XxlJobProperties;
import com.seal.commons.quartz.xxljob.XxlJobQuartzJobHandlerAutoExporter;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 功能描述: <br/>
 *
 * @author yong.huang
 * @date: 2020-10-28 16:11<br/>
 * @since JDK 1.8
 */
@Configuration
@EnableConfigurationProperties({XxlJobProperties.class})
public class XxlJobAutoConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(XxlJobAutoConfiguration.class);

    @Value("${spring.application.name}")
    private String applicationName;

    @Bean
    public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties prop) {
        LOGGER.info(">>>>>>>>>>> xxl job config init...");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(prop.getAdminAddress());
        xxlJobSpringExecutor.setAppname(applicationName);
        xxlJobSpringExecutor.setIp(prop.getIp());
        xxlJobSpringExecutor.setPort(prop.getPort());
        xxlJobSpringExecutor.setAccessToken(prop.getAccessToken());
        xxlJobSpringExecutor.setLogPath(prop.getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(prop.getLogRetentionDays());
        return xxlJobSpringExecutor;

    }

    @Bean
    public XxlJobQuartzJobHandlerAutoExporter xxlJobQuartzJobHandlerAutoExporter(){
        LOGGER.info(">>>>>>>>>>> xxl job exporter init...");
        return new XxlJobQuartzJobHandlerAutoExporter();
    }

}